package com.mc.lp.database.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.mc.lp.core.util.CollectionUtil;
import com.mc.lp.database.dao.IAcademicRecordDAO;
import com.mc.lp.database.entity.AcademicRecordEntity;
import com.mc.lp.database.entity.StudentRecordEntity;
import com.mc.lp.shared.vo.SearchCriteriaVO;

/*
 * @author aphanda
 * @date Sep 14, 2014
 * @time 1:59:28 AM
 */

/**
 * @author aphanda
 * 
 */
@Repository
public class AcademicRecordDAOImpl extends
		AbstractGenericHibernateDAO<AcademicRecordEntity, Long, IAcademicRecordDAO> implements
		IAcademicRecordDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mc.lp.database.dao.IAcademicRecordDAO#saveAcademics()
	 */
	public void saveAcademics(AcademicRecordEntity e) {
		// TODO Auto-generated method stub
		/*
		 * Query q = getCurrentSession().createQuery(
		 * "select cat.name, cat from DomesticCat cat " + "order by cat.name");
		 */

		StudentRecordEntity r = new StudentRecordEntity();
		Long regisId = r.getRegistrationID().getId();
		Long gradeID = r.getSchoolGradeID().getId();
		Integer year = r.getYear();
		Integer attachmentId = 123;
		Query studentEntryQuery = getCurrentSession()
				.createQuery(
						"insert into STUDENT_RECORD (REGISTRATION_ID,YEAR,GRADE_ID ,ATTACHMENT_ID ) values (?, ?, ?, ?)");
		studentEntryQuery.setParameter(0, regisId);
		studentEntryQuery.setParameter(1, gradeID);
		studentEntryQuery.setParameter(2, year);
		studentEntryQuery.setParameter(3, attachmentId);

		int test = studentEntryQuery.executeUpdate();

		System.out.println("Execute Test " + test);
		/*
		 * String hqlInsert =
		 * "insert into ACEDMIC_RECORD (id, name) select c.id, c.name from Customec where ..."
		 * ; int createdEntities = s.createQuery( hqlInsert ) .executeUpdate();
		 */

	}

	public List<AcademicRecordEntity> getAcademicsRecordsByGradeAndTerm(String grade, Integer termId) {
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(AcademicRecordEntity.class)
				.add(Restrictions.eq("TERM_ID", termId)).add(Restrictions.eq("GRADE", grade));
		acadList = crit.list();
		return acadList;
	}

	public List<AcademicRecordEntity> fetchDetailsByEntityIdTermGrade(Long entityId, int gradeId,
			int termId) {
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = this.getCurrentSession().createCriteria(AcademicRecordEntity.class);
		crit.add(Restrictions.eq("termId", termId));
		crit.add(Restrictions.eq("gradeId", gradeId));
		crit.add(Restrictions.eq("studentEntity",entityId));
		acadList = crit.list();
		return acadList;
	}
	
	public List<AcademicRecordEntity> fetchDetailsByEntityIdYear(Long entityId, int year, int limit) {
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(AcademicRecordEntity.class);
		crit.add(Restrictions.between("year", (Long.valueOf(year)-Long.valueOf(limit)), Long.valueOf(year+1)));
		crit.add(Restrictions.eq("studentEntity",entityId));
		acadList = crit.list();
		return acadList;
	}
	
	
	@Override
	public List<AcademicRecordEntity> fetchAcademicsByGrades(Long studentEntity,
			List<Integer> gradeValueList) throws DataAccessException {
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(AcademicRecordEntity.class);
		crit.add(Restrictions.in("gradeId", gradeValueList));
		crit.add(Restrictions.eq("studentEntity",studentEntity));
		acadList = crit.list();
		return acadList;
	}
	@Override
	public List<AcademicRecordEntity> fetchAcademicsBySearchCriteriaForAdvancedGraph(SearchCriteriaVO searchCrit) throws DataAccessException {
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(AcademicRecordEntity.class);
		
		try {
			if (null != searchCrit.getSelectedGrade() && searchCrit.getSelectedGrade().length > 0) {

				crit.add(Restrictions.in("gradeId",	CollectionUtil.convertStringArraytoIntArray(searchCrit.getSelectedGrade())));

			}

			if (null != searchCrit.getSelectedTerm() && searchCrit.getSelectedTerm().length > 0) {
				crit.add(Restrictions.in("termId", CollectionUtil.convertStringArraytoIntArray(searchCrit.getSelectedTerm())));
			}
			// AP Check for null in the crit
			if (null != searchCrit.getSelectedSubjectList()	&& searchCrit.getSelectedSubjectList().length > 0) {
				crit.add(Restrictions.in("subject.id", CollectionUtil.convertStringArraytoLongArray(searchCrit.getSelectedSubjectList())));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		crit.add(Restrictions.eq("studentEntity",searchCrit.getStudentEntityId()));
		
		acadList = crit.list();
		
		return acadList;
	}
	
	
	
	
	
	public List<AcademicRecordEntity> fetchDetailsByEntityIdTerm(Long entityId, int year, int termId, int limit) {
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(AcademicRecordEntity.class);
		crit.add(Restrictions.between("year", (Long.valueOf(year)-Long.valueOf(limit)), Long.valueOf(year+1)));
		crit.add(Restrictions.eq("studentEntity",entityId));
		
		if(termId!=-1)
		crit.add(Restrictions.eq("termId",termId));
		
		acadList = crit.list();
		return acadList;
	}
	
	public List<AcademicRecordEntity> getAllAcadsByStudentEntity(Long studentEntity){
		List<AcademicRecordEntity> acadList = new ArrayList<AcademicRecordEntity>();
		Criteria crit = getCurrentSession().createCriteria(AcademicRecordEntity.class);
		crit.add(Restrictions.eq("studentEntity",studentEntity));
		acadList = crit.list();
		return acadList;
		
	}
}
